package com.itheima.rabbitmq.config;



import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author : [chenxys]
 * @version : [v1.0]
 * @Description : rabbitmq配置类
 * @createTime : [2022/6/24 12:59]
 */
@Configuration
public class RabbitMqConfig {
    public static final String EXCHANGE_NAME = "boot_topic_exchange";
    public static final String QUEUE_NAME = "boot_queue";

    /**
     * 交换机
     * @author chenxys
     * @return org.springframework.amqp.core.Exchange
     */
    @Bean("bootExchange")
    public Exchange bootExchange(){
        //                      topic类型交换机  激活码名称    是否持久化      构建
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }

    /**
     * 队列
     * @author chenxys
     * @return org.springframework.amqp.core.Queue
     */
    @Bean("bootQueue")
    public Queue bootQueue(){
        //                          队列名称     构建
        return QueueBuilder.durable(QUEUE_NAME).build();
    }

    /**
     * 队列与交换机绑定
     *      1、知道那个队列
     *      2、知道那个交换机
     *      3、设置路由键[routingKey]
     * @author chenxys
     * @return
     */
    @Bean
    public Binding bindingQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){
        //                     绑定队列     到交换机      通过 "boot.#"这个路由键    没有更多的参数
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }
}
